﻿collegeApp.controller('CollegeController', function ($scope, CollegeService, $routeParams)
{
    $scope.colleges = [];
    $scope.reviews = [];
    $scope.review = {};

    CollegeService.getColleges().then(function (data) {
        $scope.colleges = data;
    }, function () {
        console.log('error - did not work');
    });

    var id = $routeParams.id;
    $scope.college = CollegeService.getCollege(id); // should move up into the then part of the CollegeService.getColleges call
    
    // Get all reviews then get the reviews for our school
    CollegeService.getReviews().then(function (data) {
     }, function () {
        console.log('error - did not work');
    });
    $scope.reviews = CollegeService.getCollegeReviews(id);
    
    $scope.getColleges = function () {
        CollegeService.getColleges().then(function (data) {
            $scope.colleges = data;
            
        }, function () {
            console.log('error - did not work');
        });
    }

    //$scope.getCollege = function (id) {
    //    CollegeService.getColleges(id).then(function (data) {
    //        $scope.colleges = data;
    //    }, function () {
    //        console.log('error - did not work');
    //    });
    //}

    $scope.addCollege = function (college) {
        CollegeService.addCollege(college).then(function () {
            college.Name = "";
            college.City = "";
            college.State = "";
            $scope.getColleges();
        }, function () {
            console.log('error - did not work');
            
        });
    }

    $scope.deleteCollege = function (id) {
        CollegeService.deleteCollege(id).then(function () {
            $scope.getColleges();
            college.Name = "";
            college.City = "";
            college.State = "";
         
        }, function () {
            console.log('error - did not work');

        });
    }

    $scope.saveCollege = function (college) {
        CollegeService.saveCollege(college).then(function () {
            //college.Name = "";
            //college.City = "";
            //college.State = "";
            $scope.getColleges();
        }, function () {
            console.log('error - did not work');

        });
    }

    $scope.addReview = function (review) {
        review.CollegeId = id;
        CollegeService.addReview(review).then(function () {
            //college.Name = "";
            //college.City = "";
            //college.State = "";
            $scope.getColleges();
            CollegeService.getReviews(id).then(function (data) {
                $scope.reviews = CollegeService.getCollegeReviews(id);
            }, function () {
                console.log('error - did not work');
            });
        }, function () {
            console.log('error - did not work');

        });
    }
    

});